package com.lw.controller;

import com.lw.system.ProfileResult;
import io.jsonwebtoken.Claims;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseController {
    //@ModelAttribute 控制器方法执行之前执行
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected String companyId;
    protected String companyName;
    //使用jwt获取
//    @ModelAttribute
//    public void set(HttpServletRequest request,HttpServletResponse response){
//        Claims claims = (Claims) request.getAttribute( "claims" );
//        this.request=request;
//        this.response=response;
//        if (claims!=null){
//            this.companyId= (String) claims.get( "companyId" );
//            this.companyName= (String) claims.get( "companyName" );
//        }
//    }
    //使用shiro获取
        @ModelAttribute
    public void set(HttpServletRequest request,HttpServletResponse response){
        this.request=request;
        this.response=response;
        Subject subject= SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (principals!=null&&!principals.isEmpty()){
            ProfileResult result = (ProfileResult) principals.getPrimaryPrincipal();
            this.companyId=result.getCompanyId();
            this.companyName=result.getCompany();
        }
    }
}
